﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Xml;
using Aandelendelen.String;

namespace Aandelendelen.Ad
{
	public class AdvertiseOptionCollection : Collection<AdvertiseOption>
	{
		public AdvertiseOptionCollection()
		{
			this.ReadAdvertiseOptions();
		}

		private void ReadAdvertiseOptions()
		{
			string path = HttpContext.Current.Server.MapPath("/App_Data/AdvertiseOptions.xml");

			XmlDocument doc = new XmlDocument();

			doc.Load(path);

			XmlNodeList nodes = doc.SelectNodes("/aandelendelen/option");

			if (nodes != null)
			{
				foreach (XmlNode node in nodes)
				{
					AdvertiseOption advertiseOption = this.ReadAdvertiseOption(node);

					this.Add(advertiseOption);
				}
			}
		}

		private AdvertiseOption ReadAdvertiseOption(XmlNode node)
		{
			string title = node.Attributes["title"].Value;
			int id = XmlConvert.ToInt32(node.Attributes["id"].Value);
			string description = null;
			AdvertiseCostCollection advertiseCosts = null;

			foreach (XmlNode childNode in node.ChildNodes)
			{
				switch (childNode.Name)
				{
					case "description": description = childNode.InnerXml.Clean();
						break;
					case "costs": advertiseCosts = this.ReadAdvertiseCosts(childNode);
						break;
				}
			}

			AdvertiseOption option = new AdvertiseOption(title, description, id, advertiseCosts);

			return option;
		}

		private AdvertiseCostCollection ReadAdvertiseCosts(XmlNode node)
		{
			AdvertiseCostCollection advertiseCosts = new AdvertiseCostCollection(node.Attributes["titledesc"].Value, node.Attributes["titlecost"].Value);

			foreach (XmlNode childNode in node.ChildNodes)
			{
				switch (childNode.Name)
				{
					case "cost": advertiseCosts.Add(this.ReadAdvertiseCost(childNode));
						break;
				}
			}

			return advertiseCosts;
		}

		private AdvertiseCost ReadAdvertiseCost(XmlNode node)
		{
			string description = null;
			int from = 0;
			int to = 0;
			decimal cost = 0;
			string costPrefix = null;

			XmlAttribute attr = node.Attributes["description"];
			if (attr != null)
				description = attr.Value;

			attr = node.Attributes["from"];
			if (attr != null)
				from = XmlConvert.ToInt32(attr.Value);

			attr = node.Attributes["to"];
			if (attr != null)
				to = XmlConvert.ToInt32(attr.Value);

			attr = node.Attributes["cost"];
			if (attr != null)
				cost = XmlConvert.ToDecimal(attr.Value);

			attr = node.Attributes["costprefix"];
			if (attr != null)
				costPrefix = attr.Value;

			return new AdvertiseCost(description, from, to, cost, costPrefix);
		}

		public new AdvertiseOption this[int id]
		{
			get
			{
				foreach (AdvertiseOption option in this)
				{
					if (option.Id == id)
						return option;
				}

				return null;
			}
		}

		public decimal GetBannerViewsCost(int numberDividedBy1000)
		{
			AdvertiseOption bannerViewsOption = this[1];

			foreach (AdvertiseCost cost in bannerViewsOption.AdvertiseCosts)
			{
				if (numberDividedBy1000 >= cost.From && numberDividedBy1000 <= cost.To)
				{
					return (decimal)numberDividedBy1000 * cost.Cost;
				}
			}

			return 0;
		}
	}
}